using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HermesPortal.Core;  

public partial class Admin_Controls_EditProfile : System.Web.UI.UserControl
{
    private string _userName = "";
    public string UserName
    {
        get { return _userName; }
        set { _userName = value; }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        this.Page.RegisterRequiresControlState(this);
    }

    protected override void LoadControlState(object savedState)
    {
        object[] ctlState = (object[])savedState;
        base.LoadControlState(ctlState[0]);
        this.UserName = (string)ctlState[1];
    }

    protected override object SaveControlState()
    {
        object[] ctlState = new object[2];
        ctlState[0] = base.SaveControlState();
        ctlState[1] = this.UserName;
        return ctlState;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            ddlCountries.DataSource = HermesPortalUI.UIHelpers.GetCountries();
            ddlCountries.DataBind();

            
            
            // if the UserName property contains an emtpy string, retrieve the profile
            // for the current user, otherwise for the specified user
            ProfileCommon profile = this.Profile;
            if (this.UserName.Length > 0)
                profile = this.Profile.GetProfile(this.UserName);


            // Create list contianing all the current sagas
            listSagas.DataTextField = "Value";
            listSagas.DataValueField = "Key"; 
            listSagas.DataSource = HermesPortalUI.UIHelpers.GetSagaList();
            listSagas.DataBind();
            if (this.Page.User.Identity.IsAuthenticated)
            {
                Creator creator = null;
                if (this.UserName.Length > 0)
                    creator = UserServices.GetCreatorByUserName(this.UserName);
                else
                    creator = UserServices.GetCreatorByUserName(profile.UserName);

                if (creator == null)
                    throw new ApplicationException(String.Format("Erro: No Creator object was found for User {0} ", profile.UserName));
                if (creator.DefaultSagaID > 0)
                {
                    ListItem li = listSagas.Items.FindByValue(creator.DefaultSagaID.ToString());
                    if (li != null)
                    {
                        li.Selected = true;
                    }
                }
            }
         
           
            txtFirstName.Text = profile.FirstName;
            txtLastName.Text = profile.LastName;
            ddlGenders.SelectedValue = profile.Gender;
            txtStreet.Text = profile.Address.Street;
            txtCity.Text = profile.Address.City;
            txtPostalCode.Text = profile.Address.PostalCode;
            txtState.Text = profile.Address.State;
            ddlCountries.SelectedValue = profile.Address.Country;
            txtPhone.Text = profile.Contacts.Phone;
            txtFax.Text = profile.Contacts.Fax;
        }
    }

    public void SaveProfile()
    {
        // if the UserName property contains an emtpy string, save the current user's profile,
        // othwerwise save the profile for the specified user
        ProfileCommon profile = this.Profile;
        if (this.UserName.Length > 0)
            profile = this.Profile.GetProfile(this.UserName);

        profile.FirstName = txtFirstName.Text;
        profile.LastName = txtLastName.Text;
        profile.Gender = ddlGenders.SelectedValue;
        
        profile.Address.Street = txtStreet.Text;
        profile.Address.City = txtCity.Text;
        profile.Address.PostalCode = txtPostalCode.Text;
        profile.Address.State = txtState.Text;
        profile.Address.Country = ddlCountries.SelectedValue;
        profile.Contacts.Phone = txtPhone.Text;
        profile.Contacts.Fax = txtFax.Text;
          
        profile.Save();
        Creator creator = UserServices.GetCreatorByUserName(profile.UserName);

        if (listSagas.SelectedValue != null)
        {
            if (creator == null)
                throw new ApplicationException(String.Format("Error: No Creator object was found for User {0} ", profile.UserName));
            else
            {
                creator.DefaultSagaID = Convert.ToInt32(listSagas.SelectedValue);
                UserServices.UpdateCreator(creator); 
            }
        }
    }
}
